#ifndef GMAIL_GMAILCONTEXT_H
#define GMAIL_GMAILCONTEXT_H

#include <QNetworkAccessManager>
#include <QXmlStreamReader>
#include <QTimer>

#include "twcore/IContext.h"
#include "utils/OAuthClient.h"

class QUrl;
class QDialog;
class QTextBrowser;
class QPushButton;
class QVBoxLayout;
namespace GMail
{
	class GMailFrame;
	struct GMailTag
	{
		QString name;
		QString alias;
	};
	class GMailContext: public TWCore::IContext
	{
		const static QString GMail_Scope;
		const static QString OAuth_RequestToken_URL;
		const static QString OAuth_AccessToken_URL;
		const static QString OAuth_AuthorizeToken_URL;
		const static QString GMail_Feed_URL;
		const static QString GMail_Feed_URL2;

		Q_OBJECT
		public:
			explicit GMailContext(const QByteArray& info = QByteArray());
			virtual ~GMailContext();

			const static QString ContentIconBig;
			const static QString ContentIconSmall;

			/* IContent */
			class QFrame* contentFrame();
			QByteArray saveInfo() const;
			QString detailInfo() const;

			bool setup();

			void setCheckMailInterval(int millisec){i_checkInterval = millisec;}
		private:
			QTimer					m_timer;
			QString					s_email;
			QList<GMailTag>			m_tags;
			QXmlStreamReader			m_xmlParser;
			TWUtils::OAuthClient	m_client;
			QNetworkAccessManager	m_manager;
			GMailFrame*				m_frame;
			QNetworkReply*			m_reply;
			int i_currentCheckingTagId;
			int i_checkInterval;

			QByteArray encryptInfo(const QByteArray& info)const{return info.toBase64();}
			QByteArray decryptInfo(const QByteArray& info)const{return QByteArray::fromBase64(info);}
		private slots:
			void configure();
			void checkMail();
			void onCheckMailFinished();
	};
}

#endif /* GMAIL_GMAILCONTEXT_H */
